package com.zmn.oms.dubbo.impl.zmn.serviceprovider.order;

import com.zmn.oms.common.constant.ConfOrderDutyConsts;
import org.apache.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.dubbo.dto.zmn.order.UpdateOrderDRO;
import com.zmn.oms.dubbo.dto.zmn.order.serviceprovider.*;
import com.zmn.oms.dubbo.impl.zmn.order.OrderWorkBaseRemoteService;
import com.zmn.oms.dubbo.interfaces.zmn.serviceprovider.order.ServProviderOrderOperationReomteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.order.zmn.ZsNormalAddressOrderDTO;
import com.zmn.oms.model.dto.work.masterwork.ModifyDutyTimeDTO;
import com.zmn.oms.model.dto.work.modify.other.ZsRemarkDTO;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkScheduleBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.financework.ZsNormalFinanceWorkBService;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 类描述：服务商工单操作相关
 *
 * @author yule
 * @date 2020/7/2 17:59
 */
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class ServProviderOrderOperationServiceImpl extends OrderWorkBaseRemoteService implements ServProviderOrderOperationReomteService {
    private static final String TAG = "服务商-订单操作";

    @Autowired
    private ZsNormalFinanceWorkBService zsNormalFinanceWorkBService;
    @Autowired
    ZsOrderWorkScheduleBService zsOrderWorkScheduleBService;

    @Override
    public ResponseDTO<UpdateOrderDRO> findOrderUpdateDetail(Long orderId, Long workId) {
        return super.findOrderUpdateDetail(orderId, workId);
    }

    @Override
    public ResponseDTO updateOrderDutyTime(DutyTimeDIO dutyTimeDIO) {

        logger.info("[{}] 保存服务商预约时间：{}", TAG, dutyTimeDIO);
        ModifyDutyTimeDTO modifyDutyTimeDTO = BeanMapper.map(dutyTimeDIO, ModifyDutyTimeDTO.class);
        try {

            // 验证预约时间范围
            zsOrderWorkScheduleBService.validateDutyTime(dutyTimeDIO.getOrderId(), dutyTimeDIO.getPlat(), ConfOrderDutyConsts.TYPE_SERVICE_PROVIDER, dutyTimeDIO.getDutyTime());

            zsOrderWorkBService.updateOrderDutyTime(modifyDutyTimeDTO);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("updateOrderDutyTime error：{}", e.getMessage(), e);
            return ResponseDTO.fail(e.getMessage());
        }
        return ResponseDTO.success("保存成功");
    }

    @Override
    public ResponseDTO updateOrderAddress(UpdateAddressDIO updateAddressDIO) {
        logger.info("[{}] 保存服务商修改地址：{}", TAG, updateAddressDIO);
        ZsNormalAddressOrderDTO addressOrderDTO = BeanMapper.map(updateAddressDIO, ZsNormalAddressOrderDTO.class);
        try {
            zsNormalOrderBService.updateAddress(addressOrderDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseDTO.fail(e.getMessage());
        }
        return ResponseDTO.success("保存成功");
    }

    @Override
    public ResponseDTO saveRemark(OrderRemarkDIO remarkDIO) {
        logger.info("[{}] 保存服务商备注：{}", TAG, remarkDIO);

        // 服务商备注
        if (StringUtil.isNotBlank(remarkDIO.getServiceProviderRemark())) {
            ZsRemarkDTO dto = BeanMapper.map(remarkDIO, ZsRemarkDTO.class);
            zsNormalOrderBService.saveServiceProviderRemark(dto);
        }

        return ResponseDTO.success("保存成功");
    }

}
